package cn.flying.cloud.base.common.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import java.util.HashSet;
import java.util.Set;

/**
 * 自定义集合码值校验器
 *
 * @author: admin
 * @date: 2023年01月31日 20:54
 * @version: 1.0
 */
public class ListValueConstrainValidator implements ConstraintValidator<ListValue, String> {

    private Set<String> valSet = new HashSet<>();

    /**
     * 初始化方法
     *
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        String[] values = constraintAnnotation.values();
        for (String value : values) {
            valSet.add(value);
        }
    }

    /**
     * @param value   object to validate 需要校验的值
     * @param context context in which the constraint is evaluated
     * @return {@code false} if {@code value} does not pass the constraint
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return valSet.contains(value);
    }
}